<template>
  <div>
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('cancel')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { updateUserProfile } from '@/api/user.js'
import { Toast } from 'vant'
export default {
  data () {
    return {
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },

  props: {
    value: {
      type: String,
      required: true
    }
  },

  created () {

  },

  methods: {
    async onConfirm (val) {
      const time = dayjs(val).format('YYYY-MM-DD')
      try {
        await updateUserProfile({
          birthday: time
        })
        Toast('更新成功')
        this.$emit('cancel')
        this.$emit('input', time)
      } catch (err) {
        Toast('更新失败')
      }
    }
  }
}
</script>

<style scoped>

</style>
